// 人物接口,导入express框架，使用router,让每一个router都相当于小的app实例
let express = require("express")
let person_router = express.Router();
let sqlQuery = require("../mysql/mysql");
// 处理post请求的中间件，用于解析post返回的数据
const bodyParser = require('body-parser')
person_router.use(bodyParser.urlencoded({extended:false}))
// 1.按照id查询
person_router.get('/id',async(req,res) => {
  let strSql = "select * from onepice where id = ? ";
  let id = req.query.id;//params拿到浏览器带过来得请求参数，如果是ajax请求，则用res.query.id
  // console.log("查询的id="+req.query.id)
  let results = await sqlQuery(strSql ,[id]);// 把sql语句传入，并且传入占位符的参数
  // console.log(results);
  // 允许跨域
  res.append("Access-Control-Allow-Origin", "*");
  res.append("Access-Control-Allow-Content-type","*")
  res.json(Array.from(results));//查询到的数据数组化，并返回json数据
})
//2.按照请求次数，一次返回三十条
let page = 0;
person_router.get('/',async(req,res) => {
  page = req.query.page;
  console.log("res.body,page="+page)
  let strSql = "select * from onepice limit ?,30";//limit,从第几条开始，要拿多少条数据
  let results = await sqlQuery(strSql,[30*page]);// 把sql语句传入
  if(results.length==0){res.json({ status: 2, msg: '数据库已空！' })}
  // let options = {
  //   person:await Array.from(results),
  // }
  // res.json(options)
   // 允许跨域
   res.append("Access-Control-Allow-Origin", "*");
   res.append("Access-Control-Allow-Content-type","*")
  res.json(Array.from(results));//查询到的数据数组化，并返回json数据
})

//3.按照名字来请求人物
person_router.get('/users',async(req,res) => {
  let users = ' '+ req.query.users;
  let strSql = "select * from onepice where users = ?";//limit,从第几条开始，要拿多少条数据
  let results = await sqlQuery(strSql,[users]);// 把sql语句传入
  // if(results.length==0){res.json({ status: 2, msg: '没有找到！' })}
  // let options = {
  //   person:await Array.from(results),
  // }
  // res.json(options)
  console.log("users="+users)
  if(users==undefined){
    console.log("瞎几把输入")
    return []
  }
  if(results.length==0){
    console.log('666')
  }else{
    console.log('777')
  }
   // 允许跨域
   res.append("Access-Control-Allow-Origin", "*");
   res.append("Access-Control-Allow-Content-type","*")
  res.json(Array.from(results));//查询到的数据数组化，并返回json数据
})

//4.按照名字来模糊查询，来请求人物
person_router.get('/user_dim',async(req,res) => {
  let users = req.query.user_dim;
  console.log("user_dim="+users)
  // if(user_dim==undefined){
  //   console.log("瞎几把输入")
  //   return []
  // }
  let strSql = "select * from onepice where users like ?";//limit,从第几条开始，要拿多少条数据
  let results = await sqlQuery(strSql,[users]);// 把sql语句传入
  // let results = await sqlQuery(strSql);// 把sql语句传入
  // if(results.length==0){res.json({ status: 2, msg: '查询不到！' })}
  // let options = {
  //   person:await Array.from(results),
  // }
  // res.json(options)
  console.log(results)
   // 允许跨域
   res.append("Access-Control-Allow-Origin", "*");
   res.append("Access-Control-Allow-Content-type","*")
  res.json(Array.from(results));//查询到的数据数组化，并返回json数据
})
//5.添加用户接口
person_router.get('/addUser',async(req,res) => {
  let add_user = req.query.add_user;
  let add_image = req.query.add_image
  let add_href = req.query.add_href;
  let arr =[add_user,add_image,add_href];
  let strSql = 'INSERT INTO onepice VALUES(null,?,?,?)';
  let results = await sqlQuery(strSql,arr);
  console.log(arr)
  // INSERT INTO onepice VALUES(null,'凯', 'http://img5.imgtn.bdimg.com/it/u=3847036044,2307143031&fm=26&gp=0.jpg','http://hu66.html')
   // 允许跨域
   res.append("Access-Control-Allow-Origin", "*");
   res.append("Access-Control-Allow-Content-type","*")
  res.json({ status: 200, msg: '成功添加用户！' })
})
//6.删除用户接口
person_router.get('/deleteUser',async(req,res) => {
  let delete_id = req.query.id;
  console.log("删除的id="+delete_id)
  let strSql = 'DELETE from onepice where id = ?';
  let results = await sqlQuery(strSql,[delete_id]);
  console.log(results)
  // INSERT INTO onepice VALUES(null,'凯', 'http://img5.imgtn.bdimg.com/it/u=3847036044,2307143031&fm=26&gp=0.jpg','http://hu66.html')
   // 允许跨域
   res.append("Access-Control-Allow-Origin", "*");
   res.append("Access-Control-Allow-Content-type","*")
  res.json({ status: 200, msg: '成功删除用户,id='+delete_id+'！' })
})

//7.修改用户接口
person_router.get('/updateUser',async(req,res) => {
  let update_id = req.query.id
  let update_user = req.query.user;
  if(update_user==undefined){
    return 0;
  }
  let update_image = req.query.img
  let update_href = req.query.href;
  let arr =[update_user,update_image,update_href,update_id];
  console.log("修改的id="+update_id)
  let strSql = 'UPDATE onepice SET users = ?, image=?,href = ? WHERE id = ?';
  let results = await sqlQuery(strSql,arr);
  console.log(results)
  // INSERT INTO onepice VALUES(null,'凯', 'http://img5.imgtn.bdimg.com/it/u=3847036044,2307143031&fm=26&gp=0.jpg','http://hu66.html')
   // 允许跨域
   res.append("Access-Control-Allow-Origin", "*");
   res.append("Access-Control-Allow-Content-type","*")
  res.json({ status: 200, msg: '成功修改用户,id='+update_id+'！' })
})

module.exports = person_router